##@file molecules/energy.tcl
# The commands to get energy
# \verbatim

##get current energy from CHARMM interpreter
oo::define Charmm method getEnergy {} {
    my variable ener
    set enerblock [ my sendCmd "energy"]
    if  [ regexp  {ENER>[^\n]+\n} $enerblock match ] {
	set elist [regexp -inline -all -- {\S+} $match]
	set ener(ENER) [lindex $elist 2]
	set ener(deltaE) [lindex $elist 3]
	set ener(GMRS) [lindex $elist 4]
    }
    
    if  [ regexp  {ENER\s+INTERN>[^\n]+\n} $enerblock match ] {
	set elist [regexp -inline -all -- {\S+} $match]
	set ener(BOND) [lindex $elist 2]
	set ener(ANGL) [lindex $elist 3]
	set ener(UREY) [lindex $elist 4]
	set ener(DIHE) [lindex $elist 5]
	set ener(IMPR) [lindex $elist 6]
    }
    if  [ regexp  {ENER\s+EXTERN>[^\n]+\n} $enerblock match ] {
	set elist [regexp -inline -all -- {\S+} $match]
	set ener(VDW) [lindex $elist 2]
	set ener(ELEC) [lindex $elist 3]
	set ener(HBOND) [lindex $elist 4]
	set ener(ASP) [lindex $elist 5]
	set ener(USER) [lindex $elist 6]
    }
    if  [ regexp  {ENER\s+MMFP>[^\n]+\n} $enerblock match ] {
	set elist [regexp -inline -all -- {\S+} $match]
	set ener(GEO) [lindex $elist 2]
	set ener(MDIP) [lindex $elist 3]
	set ener(SSBP) [lindex $elist 4]
	set ener(SHEL) [lindex $elist 5]
	set ener(DROFfa) [lindex $elist 6]
    } else { set ener(GEO) 0.0;set ener(MDIP) 0.0;set ener(SSBP) 0.0;
    	set ener(SHEL) 0.0; set ener(DROFfa) 0.0;
    }
    return [array get ener]
}

## minimize the energy by several steps
oo::define Charmm method minimize {type nstep} {
    my variable ener
    my variable interface
    set enerblock [my sendCmd "mini $type nstep $nstep"]
    #supported type
    array set Key { abnr ABNR sd STPD }
    set keyword Key($type)
    if  [ regexp  "$Key($type)>\[^\\n\]+\\n" $enerblock match ] {
	set elist [regexp -inline -all -- {\S+} $match]
	set ener(nstep) [lindex $elist 1]
	set ener(ENER) [lindex $elist 2]
	set ener(deltaE) [lindex $elist 3]
	set ener(GMRS) [lindex $elist 4]
    }
    
    if  [ regexp   "$Key($type)\\s+INTERN>\[^\\n\]+\\n" $enerblock match ] {
	set elist [regexp -inline -all -- {\S+} $match]
	set ener(BOND) [lindex $elist 2]
	set ener(ANGL) [lindex $elist 3]
	set ener(UREY) [lindex $elist 4]
	set ener(DIHE) [lindex $elist 5]
	set ener(IMPR) [lindex $elist 6]
    }
    if  [ regexp  "$Key($type)\\s+EXTERN>\[^\\n\]+\\n" $enerblock match ] {
	set elist [regexp -inline -all -- {\S+} $match]
	set ener(VDW) [lindex $elist 2]
	set ener(ELEC) [lindex $elist 3]
	set ener(HBOND) [lindex $elist 4]
	set ener(ASP) [lindex $elist 5]
	set ener(USER) [lindex $elist 6]
    }
    return [array get ener]
}
